package com.cw.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cw.entity.ExamineSpecial;
import com.cw.mapper3.ExamineSpecialMapper;
import com.cw.service.IExamineSpecialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.util.UUIDUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-09-03
 */
@Service
public class ExamineSpecialServiceImpl extends ServiceImpl<ExamineSpecialMapper, ExamineSpecial> implements IExamineSpecialService {

    /**
     * 插入
     * @param examineSpecial 需要插入的数据
     * @author ly
     * @since 2021-09-03 17:13
     * @return java.lang.boolean
     */
    @Override
    public boolean insertExamineSpecial(ExamineSpecial examineSpecial) {
        String clinicCode = examineSpecial.getClinicCode();
        //先删在插
        LambdaQueryWrapper<ExamineSpecial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExamineSpecial::getClinicCode, clinicCode);
        remove(lambdaQueryWrapper);

        examineSpecial.setId(UUIDUtil.uuid());
        examineSpecial.setCreateTime(LocalDateTime.now());
        return save(examineSpecial);
    }

    /**
     * 通过处方号获取需要替换的内容
     * @param clinicCode 处方号
     * @return java.Util.List<com.cw.entity.ExamineSpecial>
     * @author ly
     * @since 2021-09-03 17:20
     */
    @Override
    public ExamineSpecial getExamineSpecialByClinicCode(String clinicCode) {
        LambdaQueryWrapper<ExamineSpecial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ExamineSpecial::getClinicCode, clinicCode);

        List<ExamineSpecial> list = list(lambdaQueryWrapper);
        if (CollectionUtil.isEmpty(list)) { return new ExamineSpecial(); }
        return list.get(0);
    }


}
